using UnityEngine;
using System.Collections;

public class PokeBehave : MonoBehaviour {
	
	public float Velocidade, RVelocidade, Show1, Show2;
	string estado = "Idle";
	Vector3 ToGo;
	public Transform Alvo;
	AudioClip AC;
	AudioSource AS;
	int nome, lv;
	public bool ativo = false;
	public Ataque[] atqs;
	
	public struct Ataque{
		public string name;
		public int cooldown, cd;
	}
	
	// Use this for initialization
	void Start () {
		lv = 1;
		atqs = new Ataque[4];
		atqs[0].name = "Folhas Navalhas";
		atqs[0].cooldown = 200;
		atqs[0].cd = 0;
		atqs[1].name = "";
		atqs[2].name = "";
		atqs[3].name = "";
		ToGo = new Vector3(this.transform.position.x-100, this.transform.position.y, this.transform.position.z+100);
		nome = 0;
	}
	
	// Update is called once per frame
	void Update () {
		
		for(int i = 0; i < atqs.Length; i++){
			if(atqs[i].cd < atqs[i].cooldown){
				atqs[i].cd += 1;
			}
		}
		
		if(ativo)
		{
			ToGo = Alvo.transform.position;
			ToGo.y = this.transform.position.y;
			if(Dist(ToGo) > 20){
				estado = "Run";
			}else if(Dist(ToGo) > 15){
				estado = "Walk";
			}else if(Dist(ToGo) < 10){
				estado = "Idle";	
			}
			if(estado == "Run" || estado == "Walk"){
				IrPara(ToGo);
			}
		}
		
		if(!ativo)
		{
			estado = "Idle";
		}
		this.animation.Play(estado);
	}
	
	void IrPara(Vector3 Alvo){
		//this.transform.LookAt(Alvo);
		//this.transform.Rotate(0, 90, 0);
		this.transform.rotation = new Quaternion(this.transform.rotation.x, Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(Alvo - this.transform.position), 1*Time.deltaTime).y, this.transform.rotation.z, Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(Alvo - this.transform.position), 1*Time.deltaTime).w);
		this.transform.Rotate(0, Mathf.PI/2, 0);
		if(estado == "Walk"){
			this.rigidbody.AddForce(new Vector3(0, -20, 0));
			this.rigidbody.AddRelativeForce(new Vector3(-Velocidade, 0, 0));
		}else if(estado == "Run"){
			this.rigidbody.AddForce(new Vector3(0, -50, 0));
			this.rigidbody.AddRelativeForce(new Vector3(-RVelocidade, 0, 0));
		}
	}
	
	float Dist(Vector3 Alvo){
		Vector3 ttp = this.transform.position;
		return(Mathf.Sqrt(((ttp.x-Alvo.x)*(ttp.x-Alvo.x))+((ttp.z-Alvo.z)*(ttp.z-Alvo.z))));
	}
	
	public void Atacar(string n){
		if(n == "Folhas Navalhas"){
			for(int q = 0; q < 5+(int)(lv/10); q++){
				nome += 1;
				Object o = Instantiate(GameObject.Find("plano").transform, this.transform.position+new Vector3(5-(Random.value*10), 5+(2-(Random.value*4)), 5-(Random.value*10)), this.transform.rotation);
				o.name = "folha "+nome;
				GameObject go = GameObject.Find("folha "+nome);
				go.AddComponent("LeafRazorBehave");
			}
		}
	}
}
